from PyQt5.QtWidgets import QMainWindow, QTabWidget, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QFrame
from bank_manage_system.gui.user.account_summary_tab import AccountSummaryTab
from bank_manage_system.gui.user.transaction_operations_tab import TransactionOperationsTab
from bank_manage_system.gui.user.account_settings_tab import AccountSettingsTab
from bank_manage_system.gui.user.income_expense_analysis_tab import IncomeExpenseAnalysisTab
from PyQt5.QtGui import QFont
from PyQt5.QtCore import Qt

# 全局样式配置
PRIMARY_COLOR = "#3498db"
SECONDARY_COLOR = "#2ecc71"
BG_COLOR = "#ecf0f1"
TEXT_COLOR = "#2c3e50"

# 字体配置
HEADER_FONT = QFont("Microsoft YaHei UI", 14, QFont.Bold)
SECTION_FONT = QFont("Microsoft YaHei UI", 11, QFont.Bold)
TAB_FONT = QFont("Microsoft YaHei UI", 10, QFont.Bold)


class ClientDashboard(QMainWindow):
    def __init__(self, db, card_id):
        super().__init__()
        self.db = db
        self.card_id = card_id
        self.setWindowTitle(f"银行系统 - 卡号: {card_id}")
        self.setGeometry(100, 100, 1000, 700)  # 增加窗口尺寸

        # 获取客户信息 - 使用字典方式访问
        self.customer_info = self.get_customer_info()
        self.customer_name = self.customer_info.get("customerName", "客户") if self.customer_info else "客户"
        self.customer_id = self.customer_info.get("customerID", "") if self.customer_info else ""

        # 创建主窗口部件
        central_widget = QWidget()
        main_layout = QVBoxLayout(central_widget)
        main_layout.setSpacing(15)
        main_layout.setContentsMargins(20, 15, 20, 20)

        # 添加标题栏
        self.create_header(main_layout)

        # 添加分隔线
        separator = QFrame()
        separator.setFrameShape(QFrame.HLine)
        separator.setStyleSheet(f"background-color: {PRIMARY_COLOR}; height: 2px;")
        main_layout.addWidget(separator)

        # 创建选项卡
        self.tabs = QTabWidget()
        self.tabs.setFont(TAB_FONT)
        self.tabs.setStyleSheet("""
            QTabBar::tab {
                background: #ecf0f1;
                color: #2c3e50;
                border: 1px solid #bdc3c7;
                border-bottom-color: #bdc3c7;
                border-top-left-radius: 8px;
                border-top-right-radius: 8px;
                padding: 8px 20px;
                margin-right: 5px;
            }
            QTabBar::tab:selected {
                background: #3498db;
                color: white;
                border-bottom-color: #3498db;
            }
            QTabBar::tab:hover {
                background: #d6e6f2;
            }
            QTabWidget::pane {
                border: 1px solid #3498db;
                border-radius: 8px;
                background: white;
                padding: 10px;
            }
        """)

        # 账户概览
        self.summary_tab = AccountSummaryTab(db, card_id)
        self.tabs.addTab(self.summary_tab, "账户概览")

        # 交易操作
        self.transaction_tab = TransactionOperationsTab(db, card_id)
        self.tabs.addTab(self.transaction_tab, "交易操作")

        # 账户设置
        self.settings_tab = AccountSettingsTab(db, card_id)
        self.tabs.addTab(self.settings_tab, "账户设置")

        # 收支分析
        self.analysis_tab = IncomeExpenseAnalysisTab(db, card_id)
        self.tabs.addTab(self.analysis_tab, "收支分析")

        main_layout.addWidget(self.tabs)
        self.setCentralWidget(central_widget)

        # 应用全局样式
        self.apply_global_styles()

    def create_header(self, layout):
        """创建标题栏布局"""
        header_layout = QHBoxLayout()
        header_layout.setAlignment(Qt.AlignLeft | Qt.AlignTop)

        # 主标题
        title_label = QLabel("银行管理系统 - 客户面板")
        title_label.setFont(HEADER_FONT)
        header_layout.addWidget(title_label)

        # 客户信息区域
        header_layout.addStretch()
        info_layout = QHBoxLayout()
        info_layout.setSpacing(10)


        # 卡号信息
        card_label = QLabel("卡号:")
        card_label.setFont(SECTION_FONT)
        info_layout.addWidget(card_label)

        card_value = QLabel(self.card_id)
        card_value.setFont(SECTION_FONT)
        card_value.setStyleSheet(f"""
            background-color: rgba(155, 89, 182, 0.2); 
            padding: 5px 10px; 
            border-radius: 4px;
        """)
        info_layout.addWidget(card_value)

        header_layout.addLayout(info_layout)
        layout.addLayout(header_layout)

    def get_customer_info(self):
        """获取客户信息 """
        query = """
        SELECT 
            u.customerID,
            u.customerName,
            u.telephone,
            u.address,
            c.savingID,
            c.balance
        FROM cardinfo c
        JOIN userinfo u ON c.customerID = u.customerID
        WHERE c.cardID = %s
        """
        result = self.db.execute_query(query, (self.card_id,))

        # 返回字典格式，便于安全访问
        if result:
            # 元组列表
            columns = ["customerID", "customerName", "telephone", "address", "cardType", "balance"]
            return dict(zip(columns, result[0]))
        return None

    def apply_global_styles(self):
        """应用全局样式表"""
        self.setStyleSheet(f"""
            QWidget {{
                background-color: {BG_COLOR};
                color: {TEXT_COLOR};
                font-family: "Microsoft YaHei UI";
            }}

            QGroupBox {{
                border: 1px solid {PRIMARY_COLOR};
                border-radius: 8px;
                margin-top: 1.5em;
                padding-top: 10px;
            }}

            QGroupBox::title {{
                subcontrol-origin: margin;
                subcontrol-position: top left;
                padding: 4px 12px;
                background-color: {PRIMARY_COLOR};
                color: white;
                border-radius: 4px;
                font-weight: bold;
                font-size: 11pt;
            }}

            QLineEdit, QComboBox, QDateEdit, QDoubleSpinBox, QTextEdit {{
                background-color: white;
                border: 2px solid #a0a0a0;
                border-radius: 5px;
                padding: 8px 12px;
                min-height: 40px;
                font-size: 11pt;
                font-weight: 500;
                color: #333333;
            }}

            QLineEdit:focus, QComboBox:focus, QDateEdit:focus, QDoubleSpinBox:focus {{
                border: 2px solid {PRIMARY_COLOR};
            }}

            QPushButton {{
                background-color: {PRIMARY_COLOR};
                color: white;
                border-radius: 5px;
                padding: 10px 20px;
                min-width: 100px;
                min-height: 40px;
                font-weight: bold;
                font-size: 11pt;
            }}

            QPushButton:hover {{
                background-color: #2980b9;
            }}

            QPushButton:pressed {{
                background-color: #1d6fa5;
            }}

            QPushButton:disabled {{
                background-color: #bdc3c7;
            }}

            QTableWidget {{
                background-color: white;
                gridline-color: #d0d0d0;
                border-radius: 6px;
            }}

            QHeaderView::section {{
                background-color: {PRIMARY_COLOR};
                color: white;
                padding: 10px;
                font-size: 11pt;
                font-weight: bold;
                border: none;
            }}

            QLabel {{
                font-weight: bold;
                font-size: 11pt;
            }}
        """)